
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
     <include file="Index/head" />
    <link href="__PUBLIC__/css/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="__PUBLIC__/js/jquery-ui.css">
    <!-- // <script src="//code.jquery.com/jquery-1.10.2.js"></script> -->
  </head>
  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
<!--           <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button> -->
          <a class="navbar-brand" href="#">数据字典</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
<!--             <li><a href="#">Dashboard</a></li>
            <li><a href="#">Settings</a></li>
            <li><a href="#">Profile</a></li>
            <li><a href="#">Help</a></li> -->
          </ul>
          <form class="navbar-form navbar-right">
            <input type="text" class="form-control" placeholder="Search...">
          </form>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <include file="Index/menu" />
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h2 class="sub-header">字段表</h2>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>字段名(英文)</th>
                  <th>字段名(中文)</th>
                  <th>类型</th>
                  <th>主键</th>
                  <th>索引类型</th>
                  <th>空</th>
                  <th>符号</th>
                  <th>默认值</th>
                  <th>自增长</th>
                  <th>长度</th>
                  <th>备注</th>
                  <th>创建时间</th>
                </tr>
              </thead>
              <tbody class="sortable_tr">
              <volist name="list" id="vo">
                <tr class="sortable_tr_one" data-id={$vo.id} data-flid="{$vo.flid}" >
                  <td class="show_index">{$vo.fl_name}</td>
                  <td>{$vo.fl_name_ch}</td>
                  <td>{$vo.type_name}</td>
                  <td><if condition="$vo.iskey eq 1">是<else/>否</if></td>
                  <td>{$vo.index}</td>
                  <td><if condition="$vo.isnull eq 1">是<else/>否</if></td>
                  <td><if condition="$vo.isunsigned eq 1">UNSIGNED<else/>SIGNED</if></td>
                  <td>{$vo.default_value}</td>
                  <td><if condition="$vo.extra_value eq 1">是<else/>否</if></td>
                  <td>{$vo.lengths}</td>
                  <td>{$vo.comment}</td>
                  <td>{$vo.ctime|date="Y-m-d H:i:s",###}</td>
<!--                   <td style="width:54px;"><a href="{:U('Home/Fields/modify',array('flid' => $vo['flid']))}"><button type="button" class="btn btn-info">修改</button></a></td> -->
                  <td style="width:54px;"><button type="button" class="btn btn-danger" onclick="javascript:del_field({$vo.flid});">删除</button></td>
                </tr>
              </volist>
              </tbody>
            </table>
            <div> 
                <div class="form-group" style="margin-bottom:0px;">
                  <input type="text" class="form-control select_keywords" name="select_keywords" style="width:40%; display:inline;">
                  <button type="button" class="btn btn-success add_fields">添加</button>
                </div>
                <select class="form-control" id="select_options" style="width:40%; border-top:0px; display:none;">
                </select>
            </div>
          </div>
        </div>
      </div>
    </div>
      <include file="Index/footer" />
      <script src="__PUBLIC__/js/jquery-ui.js"></script>
    <!-- <link rel="stylesheet" href="/resources/demos/style.css"> -->
    <style type="text/css">
    </style>
    <script>
    $(function() {
      $(".sortable_tr").sortable({update: function(event, ui) {
        //更新的功能
          var new_rank_arr = new Array();
          $(".sortable_tr_one").each(function(index){
            new_rank_arr[index] = new Array();
            new_rank_arr[index][0] = $(this).attr("data-id");
            new_rank_arr[index][1] = index;
             // alert($(this).attr("data-id")+"new rank is "+index);
          });
          // console.log(new_rank_arr); return;
          // var rank_arr = JSON.stringify(new_rank_arr);
          var tbid = {$tbid};
          var now_url = window.location.href;
          var url = "{:U('Home/Database/update_rank')}";
          $.ajax({
              type:'post',
              url:url,
              data:{new_rank_arr:new_rank_arr,tbid:tbid},
              dataType:'json',
              success:function(data){
                  if(data==1){
                      window.location.href = now_url;
                  }
              }
          });

      }});
      $(".sortable_tr").disableSelection();
    });
    </script>
<script type="text/javascript">
/**
 * 拖动功能呢
 */
    function del_field(flid){
        var r = confirm('确定删除该字段?');
        var tbid = {$tbid};
        if(r){
            var url = "{:U('Home/Database/del_field')}";
            var now_url = window.location.href;
            $.ajax({
                type:'post',
                url:url,
                data:{flid:flid,tbid:tbid},
                dataType:'json',
                success:function(data){
                    if(data==1){
                        window.location.href = now_url;
                    }
                }
            });
        }else{
            return false;
        }
    }
    $(document).ready(function(){

        $(".select_keywords").bind('input propertychange',function(){
            $("#select_options").empty();
            var select_keywords = $(this).val();
            var url = "{:U('Home/Database/select_keywords_fields')}";
            $.ajax({
                type: 'post',
                url: url,
                data: {select_keywords:select_keywords},
                dataType: 'json',
                success: function(data){
                  var str = '';
                  $.each(data,function(k,v){

                    str += "<option value='"+v['flid']+"'>"+v['fl_name']+"("+v['fl_name_ch']+")"+"</option>";
                  });

                  var str_len = data.length;
                  $("#select_options").show();
                  $("#select_options").attr('size',str_len);
                  $("#select_options").append(str);
              }
            });
        });

        $("#select_options").bind('click',function(){
            var fl_name = $(this).find("option:selected").text();
            var flid = $(this).find("option:selected").val();
            // alert(fl_name); alert(flid);
            $(".select_keywords").val(fl_name);
            $(".add_fields").attr('flid',flid);
            $("#select_options").hide();
        });

        $(".add_fields").bind('click',function(){
            var now_url = window.location.href;
            var url = "{:U('Home/Database/add_fields_for_table')}"
            var flid = $(".add_fields").attr('flid');
            var tbid = {$tbid};
            $.ajax({
                type:'post',
                url:url,
                data:{flid:flid,tbid:tbid},
                dataType:'json',
                success:function(data){

                    if(data==1){

                        window.location.href = now_url;
                    }else if(data==2){

                        alert('字段已存在');
                    }
                }
            });
        });
    });
</script>
  </body>
</html>
